package cn.edu.ctbu.test.api;


import cn.edu.ctbu.test.domain.Student;
import cn.edu.ctbu.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.List;

@RestController
@RequestMapping("/api/login")
public class LoginRestController {

    @Autowired
    StudentService studentService;

    @GetMapping("/user")
    @ResponseBody
    public int user(HttpServletRequest httpreqeust,String username,String password){

        List<Student> students=studentService.findByNameAndPassWord(username,password);

        if(students.size()>0){
            //写入session

            HttpSession session=httpreqeust.getSession();
            session.setAttribute("userid",students.get(0).getStudentId());

            session.setAttribute("user",students.get(0));

            return 1;//成功

        }else{

            return -1;//报错
        }
    }


}
